//好友房预创建
class FriendRoom{
    constructor(docker,user){
        this.docker=docker;
        this.createTimeTick=new Date().getTime(); //创建时间
        this.users=[null,null,null,null,null,null];
        this.id=user._id;
        //this.join(user);
    }
    getMessage(){
        let obj={
            id:this.id,
            users:[]
        };
        for(let i=0;i<this.users.length;i++){
            if(!this.users[i]){
                obj.users.push(null);
            }else{
                obj.users.push(this.users[i].getMessage());
            }
        }
        return obj;
    }
    //开始游戏  type 0分组模式  1乱斗模式
    gameStart(data={type:0}){
        //如果是分组模式，判断两方是否都有人
        if(data.type===0){
            let group0User=this.users.find(item=>{return !!item&&item.group===0;});
            let group1User=this.users.find(item=>{return !!item&&item.group===1;});
            if(!group0User||!group1User){
                this.sendEvent('friendRoomExchangeError',{msg:'分组开始游戏必须双方都有队员.'});
                return;
            }
        }
        let room=this.docker.createARoom({type:1,roomType:2});
        for(let i=0;i<this.users.length;i++){
            if(!!this.users[i]){
                if(data.type==1){
                    room.type=0;
                    this.users[i].group=i;
                }
                this.users[i].joinRoom(room);
            }
        }
        this._destroy();
        room.gameStart();
    }
    //用户加入好友房
    join(user){
        if(this.users.filter(item=>{return !item}).length<=0){
            user.sendEvent('friendRoomError',{msg:'房间已满员'});
            return;
        }
        if(this.users.findIndex(item=>{return !!item&&item._id==user._id})<0){
            console.log('有人进入房间',user.nickname);
            user.friendRoom=this;
            let nullIndex=this.users.findIndex(item=>{return !item});
            user.group=nullIndex%2;
            this.users[nullIndex]=user;
            this.sendEvent('friendRoomChange',this.getMessage());
        }else{
            this.sendEvent('friendRoomChange',this.getMessage());
        }
    }
    //用户离开好友房
    exit(user){
        if(this.users.findIndex(item=>{return !!item})<0){
            this.dissolve();
        }else{
            let userIndex=this.users.findIndex(item=>{return !!item&&item._id==user._id});
            if(userIndex>=0){
                user.friendRoom=null;
                this.users[userIndex]=null;
                this.sendEvent('friendRoomChange',this.getMessage());
                if(this.users.findIndex(item=>{return !!item})<0){
                    this.dissolve();
                }
            }
        }
    }
    //交换队伍
    exchangeTeam(user){
        let userIndex=this.users.findIndex(item=>{return !!item&&item._id===user._id});
        if(userIndex<0){
            return;
        }
        let nullIndex=this.users.findIndex((item,index)=>{
            return !item&&index%2!==userIndex%2;
        });
        if(nullIndex>=0){
            this.users[userIndex]=null;
            user.group=user.group===0?1:0;
            this.users[nullIndex]=user;
            this.sendEvent('friendRoomChange',this.getMessage());
        }else{
            user.sendEvent('friendRoomExchangeError',{msg:'对面队伍已满'});
        }
    }
    //清理好友房用户，除了自己
    clearUsers(){
        for(let i=0;i<this.users.length;i++){
            if(this.users[i]._id!==id){
                this.users[i]=null;
            }
        }
    }
    //踢人
    kick(_id){
        let user=this.users.find(item=>{return !!item&&item._id==_id});
        if(!!user){
            user.sendEvent('friendRoomKick',{});
            this.exit(user);
        }

    }
    //解散房间
    dissolve(){
        this.sendEvent('friendRoomDissolve',{});
        this._destroy();
    }
    //销毁房间，不发消息给用户
    _destroy(){
        console.log('销毁自定义房间');
        for(let i=0;i<this.users.length;i++){
            if(!!this.users[i])
                this.users[i].friendRoom=null;
        }
        this.users=[null,null,null,null,null,null];
        let friendRoomIndex=this.docker.friendRooms.findIndex(item=>{return item.id==this.id});
        if(friendRoomIndex>=0){
            this.docker.friendRooms.splice(friendRoomIndex,1);
        }
    }
    sendEvent(eventName,obj){
        for(let i=0;i<this.users.length;i++){
            if(!!this.users[i])
                this.users[i].sendEvent(eventName,obj);
        }
    }
}

module.exports=FriendRoom;